package com.dk.controller;

import com.dk.mapper.StudentMapper;
import com.dk.pojo.*;
import com.dk.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")//管理员操作
public class AdminContrller {
    @Autowired
    private StudentService studentService;

    @Autowired
    private UserService userService;
    @Autowired
    private GradeService gradeService;
    @Autowired
    private ClassinfoService classinfoService;
    @Autowired
    private KcService kcService;
    HttpSession session = null;


    @ResponseBody
    @RequestMapping("/info")//展示全部学生信息
    public Map<String, Object> grinfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", studentService.FindAll());
        return map;
    }

    @ResponseBody
    @RequestMapping("/czinfo")//查找学生信息
    public Map<String, Object> czrinfo(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", studentService.FindSt(name));
        return map;
    }

    @ResponseBody//删除学生信息
    @RequestMapping("/dcrinfo")
    public int dgrinfo(@RequestBody Map<String, String> map) {
        String user = map.get("suser");
        int i = 0;
        System.out.println(user);
        System.out.println(i);
        userService.Dele(user);
        return 1;
    }

    //增加学生信息

    @RequestMapping("/zjxs")
    public String zjxs(String suser, String name, String age, String classid, String major,
                       String address, String tele, String remarks) {
        System.out.println(suser + name + address + age + classid + major + remarks);
        User s = new User();
        s.setType("Student");
        s.setUser(suser);
        s.setPassword("123456");
        userService.save(s);
        studentService.Add(suser, name, age, classid, major, address, tele, remarks);
        return "grinfo.html";

    }



    //展示所有用户信息
    @ResponseBody
    @RequestMapping("/uinfo")//查找用户信息
    public Map<String, Object> showuser() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", userService.FindAll());
        return map;
    }

    @ResponseBody
    @RequestMapping("/cjinfo")//查找成绩信息
    public Map<String, Object> showGrade() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", gradeService.FindAll());
        return map;
    }

    @ResponseBody
    @RequestMapping("/bjinfo")//查找班级信息
    public Map<String, Object> showClassinfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", classinfoService.FindAll());
        return map;
    }

    @ResponseBody
    @RequestMapping("/kc")//查找班级信息
    public Map<String, Object> showkc() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", kcService.FindAll());
        return map;
    }

    //添加user

    @RequestMapping("/adduser")
    public String adduser(User user) {
        System.out.println(user);
        userService.save(user);

        return "userinfo.html";

    }

    //增加班级
    @RequestMapping("/addc")
    public String addc(Classinfo classinfo) {
        classinfoService.add(classinfo);
        return "classinfo.html";

    }

    @RequestMapping("/addk")//增加课程
    public String addk(Kcinfo kcinfo) {
        kcService.Add(kcinfo);
        return "Kcinfo.html";

    }

    @RequestMapping("/addcj")//增成绩
    public String addcj(String suser, String grade, String classcard) {
        gradeService.Add(suser, grade, classcard);

        return "gradeinfo.html";

    }
//删除班级
    @ResponseBody
    @RequestMapping("/dc")
    public int dc(@RequestBody Map<String, String> map) {
        String classid = map.get("classid");
        System.out.println(classid);
      classinfoService.Dele(classid);
        return 1;
    }
    //删除课程
    @ResponseBody
    @RequestMapping("/dk")
    public int dk(@RequestBody Map<String, String> map) {
        String kid = map.get("kid");
        kcService.Dele(kid);
        return 1;
    }

    //删除成绩
    @ResponseBody
    @RequestMapping("/dj")
    public int dg(@RequestBody GradeUser gradeUser) {

        System.out.println(gradeUser.getGrade());
       gradeService.Dele(gradeUser);
        return 1;
    }
    //查找用户信息
    @ResponseBody
    @RequestMapping("/czuser")
    public Map<String, Object> cuser(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", userService.Find(name));
        return map;
    }
    //查找班级
    @ResponseBody
    @RequestMapping("/cclass")
    public Map<String, Object> cclass(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", classinfoService.UserFind(name));
        return map;
    }
    //查找kecheng

    @ResponseBody
    @RequestMapping("/czkc")
    public Map<String, Object> ckc(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", kcService.UserFind(name));
        return map;
    }
    //查找成绩

    @ResponseBody
    @RequestMapping("/czgrade")
    public Map<String, Object> cgrade(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", gradeService.Find(name));
        return map;
    }
    //修改user
    @RequestMapping("/xgu")
    public void cUser(@RequestBody User user) {
        System.out.println(user.getUser());
        userService.Xg(user);
    }
    //修改课程
    @RequestMapping("/xgKC")
    public void cUser(@RequestBody Kcinfo kcinfo) {
        kcService.Dele(kcinfo.getKid());
        kcService.Add(kcinfo);
    }
    //修改班级
    @RequestMapping("/cbj")
    public void cbj(@RequestBody Classinfo classinfo) {
        System.out.println(classinfo.getClassid());
        classinfoService.Xg(classinfo);
    }
    //修改学生
    @RequestMapping("/cstu")
    public void cstu(@RequestBody Student student) {

        studentService.Xg(student);
    }
    //修改成绩
    @RequestMapping("/cgrade")
    public void cgrade(@RequestBody GradeUser gradeUser) {
        gradeService.Xg(gradeUser);
    }
//课程安排打印
    @ResponseBody
    @RequestMapping("/showkcb")
    public Map<String, Object> showkcb() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", kcService.FindAllkcb());
        return map;
    }
    //增加课程
    @RequestMapping("/addkcb")
    public String addkcb(Kcardinfo kcardinfo) {
        System.out.println(kcardinfo.toString());
       kcService.Addkcb(kcardinfo);
        return "kcbinfo.html";
    }
    //删除排课
    @ResponseBody
    @RequestMapping("/dkcb")
    public int dkcb(@RequestBody Map<String, String> map) {
        int kcid = Integer.parseInt(map.get ("kcid"));
        System.out.println(kcid);
        kcService.Delekcb(kcid);
        return 1;
    }
    //查找排课信息
    @ResponseBody
    @RequestMapping("/czkcb")
    public Map<String, Object> czkcb(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 1000);
        map.put("data", kcService.UserFindkcb(name));
        return map;
    }
    //修改排课
    @ResponseBody
    @RequestMapping("/xgkcb")
    public int xgkcb(@RequestBody Kcardinfo kcardinfo) {
        kcService.Delekcb(kcardinfo.getKcid());
        kcService.Addkcb(kcardinfo);
        return 1;
    }







}
